import json
from django.shortcuts import HttpResponse
from service.senior_node_service import SeniorNodeService

#查询节点
def query_senior_node(request):
    if request.method == 'GET':
        senior_node_service = SeniorNodeService()
        res=senior_node_service.query_senior_node()
        if len(res)>0:
            data={"code":200,"msg":"查询成功","data":res}
        else:
            data={"code":500,"msg":"没查询到数据","data":res}
        return HttpResponse(json.dumps(data))

#创建节点
def create_senior_node(request):
    if request.method == 'GET':
        senior_node_service = SeniorNodeService()
        #获取前端发送的请求
        name=request.GET.get("name") #: "高管姓名",
        gender = request.GET.get("gender") #: "男|女"
        id_no = request.GET.get("id_no") #: "身份证号"
        birth_date=request.GET.get("birth_date")  #date("1980-01-01")
        position=request.GET.get("position")  #"董事长|CEO|财务总监|监事"

        res=senior_node_service.create_senior_node(
                name=name,
                id_no=id_no,
                gender=gender,
                birth_date=birth_date,
                position=position
        )
        if res == "创建节点成功":
            data={"code":200,"msg":"创建成功","data":res}
            return HttpResponse(json.dumps(data))
        else:
            data={"code":500,"msg":"创建失败","data":res}
            return HttpResponse(json.dumps(data))

#更新节点
def update_senior_node(request):
    if request.method == 'GET':
        senior_node_llm_service = SeniorNodeService()
        # 获取前端发送的请求
        name = request.GET.get("name")  #: "高管姓名",
        gender = request.GET.get("gender")  #: "男|女"
        id_no = request.GET.get("id_no")  #: "身份证号"
        birth_date = request.GET.get("birth_date")  # date("1980-01-01")
        position = request.GET.get("position")  # "董事长|CEO|财务总监|监事"
        res=senior_node_llm_service.update_senior_node(
                name=name,
                id_no=id_no,
                gender=gender,
                birth_date=birth_date,
                position=position
            )
        if res == "更新节点成功":
            data = {"code": 200, "msg": "更新成功", "data": res}
            return HttpResponse(json.dumps(data))
        else:
            data = {"code": 500, "msg": "更新失败", "data": res}
            return HttpResponse(json.dumps(data))

#删除节点
def delete_senior_node(request):
    if request.method == 'GET':
        senior_node_llm_service = SeniorNodeService()
        id_no=request.GET.get('id_no')
        res=senior_node_llm_service.delete_senior_node(id_no=id_no)
        if res=="删除节点成功":
            data={"code":200,"msg":"删除成功","data":res}
            return HttpResponse(json.dumps(data))
        else:
            data = {"code": 500, "msg": "删除失败", "data": res}
            return HttpResponse(json.dumps(data))

